//
//  TodayHotVC.m
//  EveryDayNews
//
//  Created by 徐继垚 on 14/11/27.
//  Copyright (c) 2014年 徐继垚. All rights reserved.
//

#import "TodayHotVC.h"

@interface TodayHotVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,assign)NSInteger count;
@property (nonatomic ,retain)UIButton * button2;
@property (nonatomic ,retain)UILabel *label;
@end

@implementation TodayHotVC
- (void)getData:(__unused id)sender
{
    self.dataSource = [NSMutableArray array];
    
    
    [TodayHotModel getDataWithBlock:^(NSMutableArray *array) {
        self.dataSource = array;
        [_tableView reloadData];

    }];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
        
        self.dataSource = [NSMutableArray array];
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
  
    _count = 0;
    [self buldView];
    [self getData:nil];
    [self setupRefresh];
       // Do any additional setup after loading the view.
}
-(void)buttonBack:(UIButton *)button
{
    if ([button.tintColor isEqual:[UIColor blueColor]]) {
        
        [self animangoon];
         button.tintColor = [UIColor redColor];
    }
    else
    {
        [self animanback];
        button.tintColor = [UIColor blueColor];
    }
}
-(void)animangoon
{
    [UIView animateWithDuration:0.3 animations:^{
        self.lifttableView.frame = CGRectMake(0, 0, 200 * Width, 568 * Height);
        self.tableView.frame = CGRectMake(200 * Width, 0, 320 * Width, 568 * Height);
        self.navview.frame = CGRectMake(200 * Width, 0, 320 * Width, 64 * Height);
       self.button2.frame=CGRectMake(200 * Width,15 * Height,Width*60,Height*50);
        self.label.frame = CGRectMake(200 * Width, 15 * Height, 320 * Width, 50 *Height);
    }];
}
-(void)animanback
{
    [UIView animateWithDuration:0.3 animations:^{
        self.lifttableView.frame = CGRectMake(-200, 0, 200 * Width, 568 * Height);
        self.tableView.frame = CGRectMake(0 * Width, 0, 320 * Width, 568 * Height);
         self.navview.frame = CGRectMake(0 * Width, 0, 320 * Width, 64 * Height);
        self.button2.frame=CGRectMake(0 * Width,15 * Height,Width*60,Height*50);
        self.label.frame = CGRectMake(0 * Width, 15 * Height, 320 * Width, 50 *Height);
    }];
}
-(void)buldView
{
    self.lifttableView = [[UITableView alloc]initWithFrame:CGRectMake(-200 * Width, 0, 200 * Width, 568 * Height) style:UITableViewStyleGrouped];
    self.lifttableView.delegate = self;
    self.lifttableView.dataSource = self;
    self.lifttableView.backgroundColor = [UIColor colorWithRed:35/255.0 green:42/255.0 blue:48/255.0 alpha:1];
    [self.view addSubview:_lifttableView];
    

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width, 568 * Width) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithRed:25/255.0 green:144/255.0 blue:211/255.0 alpha:1];
    self.tableView.dataSource =self;
//    self.tableView.bounces = NO;
    self.tableView.delegate  = self;
    [self.view addSubview:_tableView];

   _navview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width, 64 * Height)];
    _navview.backgroundColor = [UIColor colorWithRed:25/255.0 green:144/255.0 blue:211/255.0 alpha:1];
    _navview.alpha = 0;
    [self.view addSubview:_navview];
    
    _scrollView = [[TodayScrollView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width, 200 * Height)];
    self.tableView.tableHeaderView = _scrollView;
    
    _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button2.frame=CGRectMake(0,15 * Height,Width*60,Height*50);
    [_button2 setImage:[UIImage imageNamed:@"more.png"] forState:UIControlStateNormal];
    [_button2 addTarget:self action:@selector(buttonBack:)forControlEvents:UIControlEventTouchUpInside];
    _button2.tintColor = [UIColor blueColor];
    [self.view addSubview:_button2];
    
   _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 15 * Height, 320 * Width, 50 *Height)];
    _label.text= @"今日热闻";
    _label.textAlignment = NSTextAlignmentCenter;
    _label.textColor = [UIColor whiteColor];
    [self.view addSubview:_label];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _tableView) {
        
        CGFloat content = scrollView.contentOffset.y;
        _navview.alpha = content / 135;
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
//        if (_dataSource.count == 0) {
//            return 0;
//        }
  
        return _dataSource.count;
    }
    return 20;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        
        static NSString * identifier = @"cell";
        ContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[ContentTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.selectionStyle = NO;
        cell.myData = _dataSource[indexPath.row];
        return cell;
    }
    if (tableView == _lifttableView) {
        static NSString * identifier = @"cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        
        
        
        return cell;
    }
    return 0;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TodeayHotPushVC * vc = [[TodeayHotPushVC alloc]init];
    vc.myid = [_dataSource[indexPath.row]myid];
    [self presentViewController:vc animated:YES completion:^{

    }];
    
    
    
}
#pragma mark -
#pragma mark 刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // [self.tableView1 headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.tableView footerEndRefreshing];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉刷新";
    self.tableView.headerReleaseToRefreshText = @"松开刷新";
    self.tableView.headerRefreshingText = @"正在刷新";
    
    
    self.tableView.footerPullToRefreshText = @"上拉加载";
    self.tableView.footerReleaseToRefreshText = @"松开加载更多数据";
    self.tableView.footerRefreshingText = @"正在加载";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        //[self.tableView1 reloadData];
        
        
        [_tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView headerEndRefreshing];
    });
}
- (void)footerRereshing
{
    
    AFHTTPNetGetData * data = [[AFHTTPNetGetData alloc]init];
    [data getDataURL:[NSString stringWithFormat:@"http://news-at.zhihu.com/api/3/stories/before/2014112%lu?client=0" , _count] parameters:nil block:^(id result) {
        for (NSDictionary * dic in [result objectForKey:@"stories"]) {
            TodayHotModel * model = [[TodayHotModel alloc]init];
            model.tittle = [dic objectForKey:@"title"];
            model.myid = [dic objectForKey:@"id"];
            NSArray * arr = [dic objectForKey:@"images"];
            model.aimage = [arr objectAtIndex:0];
            [self.dataSource addObject:model];
        }
        [_tableView reloadData];
        
    }];
    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
        _count += 1;
    });
    
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
